/**
 * 
 */
package com.orcwar.engine.units;

import com.orcwar.engine.OWMap;
import com.orcwar.engine.OWPlayer;
import com.orcwar.engine.OWTile;
import com.orcwar.engine.OWUnit;
import com.orcwar.engine.tile.OWAttackTile;
import com.orcwar.enums.OWAttackTileTypeEnum;
import com.orcwar.enums.SpriteEnum;

/**
 * @author Callil
 * 
 */
public class OWTireurDeLoin extends OWUnit {

	/**
	 * Constructor
	 * @param id the id
	 */
	public OWTireurDeLoin(int id, OWPlayer player) {
		super(id, player, SpriteEnum.UNIT_TIREURDELOIN);
		
		//The fleauteur can attack 1 time
		setAttackPoints(1);
		setMaxAttackPoints(1);
		//and move up to 3 tiles
		setMaxMovePoints(3);
		setMovePoints(3);
	}
	
	@Override
	public OWAttackTile[] getGlobalMandatoryAvailableTiles(OWMap map, OWTile fromTile) {
		return null;
	}
	
	@Override
	public boolean isRangeOk(int srcX, int srcY, int targetX, int targetY) {
		//The hommelance can attack all around him at 2 tiles horizontally and
		//vertically, and at 1 tile on diagonals
		// _ _ _ _ _ _ _ _ _
		//|x| | | |x| | | |x|
		//| |x| | |x| | |x| |
		//| | |x| |x| |x| | |
		//| | | | | | | | | |
		//|x|x|x| |A| |x|x|x|
		//| | | | | | | | | |
		//| | |x| |x| |x| | |
		//| |x| | |x| | |x| |
		//|x|_|_|_|x|_|_|_|x|
		
		int xDistance = Math.abs(srcX - targetX);
		int yDistance =  Math.abs(srcY - targetY);
		
		
		boolean touchHorizontal = yDistance == 0 && (xDistance == 2 || xDistance == 3 || xDistance == 4);
		boolean touchVertical = xDistance == 0 && (yDistance == 2 || yDistance == 3 || yDistance == 4);
		boolean touchDiagonal = (xDistance == 2 && yDistance == 2) || (xDistance == 3 && yDistance == 3) || (xDistance == 4 && yDistance == 4);
		
		return touchHorizontal || touchVertical || touchDiagonal;
	}

	@Override
	public OWAttackTile[][] getAttackTiles(OWMap map, OWTile fromTile) {
		//The hommelance can attack all around him at 2 tiles horizontally and
		//vertically, and at 1 tile on diagonals
		// _ _ _ _ _ _ _ _ _
		//|x| | | |x| | | |x|
		//| |x| | |x| | |x| |
		//| | |x| |x| |x| | |
		//| | | | | | | | | |
		//|x|x|x| |A| |x|x|x|
		//| | | | | | | | | |
		//| | |x| |x| |x| | |
		//| |x| | |x| | |x| |
		//|x|_|_|_|x|_|_|_|x|
		int x;
		int y;
		if (fromTile != null) {
			x = fromTile.getX();
			y = fromTile.getY();
		} else {
			x= this.getTile().getX();
			y = this.getTile().getY();
		}
		OWAttackTile[][] attackTiles = new OWAttackTile[][]{ 
				//Diag up left
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-2, y-2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-3, y-3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-4, y-4), OWAttackTileTypeEnum.ATTACKABLE)},
				
				//Horizontal left
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-2, y), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-3, y), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL),
						OWAttackTile.create(map.getTile(x-4, y), OWAttackTileTypeEnum.ATTACKABLE)}, 
				
				//Diag bottom left
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL),
						OWAttackTile.create(map.getTile(x-2, y+2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-3, y+3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x-4, y+4), OWAttackTileTypeEnum.ATTACKABLE)},
				
				//Vertical up
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL),
						OWAttackTile.create(map.getTile(x, y-2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x, y-3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x, y-4), OWAttackTileTypeEnum.ATTACKABLE)},
				
				//Vertical down
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL),
						OWAttackTile.create(map.getTile(x, y+2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x, y+3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x, y+4), OWAttackTileTypeEnum.ATTACKABLE)},
				
				//Diag up right
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y-1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+2, y-2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+3, y-3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+4, y-4), OWAttackTileTypeEnum.ATTACKABLE)},
				
				//Horizontal right
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+2, y), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+3, y), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+4, y), OWAttackTileTypeEnum.ATTACKABLE)}, 
				
				//Diag bottom right
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y+1), OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+2, y+2), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+3, y+3), OWAttackTileTypeEnum.ATTACKABLE, OWAttackTileTypeEnum.MANDATORY_NOT_UNIT, OWAttackTileTypeEnum.MANDATORY_NOT_WALL), 
						OWAttackTile.create(map.getTile(x+4, y+4), OWAttackTileTypeEnum.ATTACKABLE)}
				};

		return attackTiles;
	}
}
